/*
 * Copyright (c) Jan Šlahora (${USER}) - 2011.
 * Created in IntelliJ IDEA 10.5
 */

package text;

/*
*   File StatusBar2
*   in package text
*   was created by slaha
*   on 17.11.11 at 18:44.
*
*/

import com.sun.org.apache.bcel.internal.generic.AALOAD;
import org.omg.CORBA.BAD_CONTEXT;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;

public class StatusBar implements Runnable {
  JPanel p;
  String s;
  Thread t;

  public StatusBar(JPanel p, String s) {
    if (p!=null && s!=null) {
      this.p = p;
      this.s = s;
      this.t = new Thread(this);
      this.t.setName("StatusBar2");
      this.t.setPriority(Math.round(Thread.MAX_PRIORITY/3));
    //  t.run();
    } else if (p!=null&& (s==null||s==""))
    {
      this.p=p;
      vynulujStatusBar(p);
    }
  }
  public synchronized  void statusBar(JPanel panel, String s) {
    Component[] comp  = panel.getComponents();
    ArrayList<Component[]> labels = new ArrayList<Component[]>();
    int[] slova = DejPocty.pocetSlov(s);
    for (Component c: comp) {
      JPanel p = (JPanel) c;
      labels.add(p.getComponents());
      if (p.getName() != null && p.getName().equals("Pnl_words")) {
        setTooltip(slova[1], p);
      }
    }
    comp = null;
    for (Component[] c: labels) {
      if (c.length > 1) {
        JLabel label = (JLabel) c[1];

        String jmeno = label.getName();
        switch (jmeno) {
          case "char_count":
            label.setText(Integer.toString(DejPocty.pocetZnaku(s)));
            break;
          case "word_count":
            label.setText(Integer.toString(slova[0]));
            break;
          case "sent_count":
            label.setText(Integer.toString(DejPocty.pocetVet2(s)));
            break;
        }
      }
    }
  }

  public static void setTooltip(int number, JPanel p) {
    String tooltip = p.getToolTipText();
    int i = DejPocty.prvniVyskyt(tooltip, new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
    if (i > 0) {
      tooltip = tooltip.substring(0, i - 2);
    }
    p.setToolTipText(tooltip + ": " + Integer.toString(number));
  }

  public synchronized void vynulujStatusBar(JPanel panel) {
    Component[] comp = panel.getComponents();
    ArrayList<Component[]> labels = new ArrayList<Component[]>();

    for (Component c : comp) {
      JPanel p = (JPanel) c;
      labels.add(p.getComponents());
      if (p.getName() != null && p.getName().equals("Pnl_words")) {
        setTooltip(0, p);
      }
    }
    comp = null;
    for (Component[] c : labels) {
      if (c.length < 1) {
        JLabel label = (JLabel) c[1];
        String jmeno = label.getName();
        switch (jmeno) {
          case "char_count":
            label.setText("0");
            break;
          case "word_count":
            label.setText("0");
            break;
          case "sent_count":
            label.setText("0");
            break;
        }
      }
    }
  }
  
  /**
   * When an object implementing interface <code>Runnable</code> is used
   * to create a thread, starting the thread causes the object's
   * <code>run</code> method to be called in that separately executing
   * thread.
   * <p/>
   * The general contract of the method <code>run</code> is that it may
   * take any action whatsoever.
   *
   * @see Thread#run()
   */
  @Override
  public void run() {
    this.statusBar(this.p, this.s);
  }

  public String getS() {
    return s;
  }
}